package com.zhl.gradledemo.consumer;


import com.rabbitmq.client.Channel;
import com.zhl.gradledemo.model.Order;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 消息接收消费
 */
@Component
public class OrderReceiver {

    //设置了接收的exchange和queue,名字相同才能收到,其他的收不到
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "order-queue", durable = "true"),
                    exchange = @Exchange(name = "order-exchange", durable = "true", type = "topic"),
                    key = "order.*")
    )
    @RabbitHandler
    public void onOrderMessage(@Payload Order order, @Headers Map<String, Object> headers, Channel channel) throws Exception {

        System.out.println("----收到消息,开始消费----");
        System.out.println("messageId:" + order.getMessageId() + ",name:" + order.getName());

        //手动接收的,接收后要返回接收成功
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        System.out.println("deliveryTag="+deliveryTag);
        channel.basicAck(deliveryTag, false);

    }


}
